import type {App} from 'vue'

// 公共方法
const utils = {
    /**
     * 截取小数点
     * @param number
     * @param fractionDigits
     * @returns
     */
    numberToFixed: function(number:string|number, fractionDigits:number = 2) {
        if (typeof(number) == "number") {
            number = number.toString();
        }
        if (number.indexOf('.') != -1) {
            let numberArr = number.split(".")
            number = numberArr[0] + (fractionDigits ? "." + numberArr[1].substring(0, fractionDigits) : "");
        }
        return number;
    },
    /**
     * 文件大小转换
     * @param size
     * @returns
     */
    getFileSize: function(size:string|number) {
        let data = "";
        const _size = typeof(size) == "number" ? size : Number.parseFloat(size);
        if (_size < 1 * 1024) {
            //如果小于0.1KB转化成B
            data = _size.toFixed(2) + "B";
        } else if (_size < 1 * 1024 * 1024) {
            //如果小于0.1MB转化成KB
            data = (_size / 1024).toFixed(2) + "KB";
        } else if (_size < 1 * 1024 * 1024 * 1024) {
            //如果小于0.1GB转化成MB
            data = (_size / (1024 * 1024)).toFixed(2) + "MB";
        } else {
            //其他转化成GB
            data = (_size / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }
        const size_str = data + "";
        const len = size_str.indexOf(".");
        const dec = size_str.substr(len + 1, 2);
        if (dec == "00") {
            //当小数点后为00时 去掉小数部分
            return size_str.substring(0, len) + size_str.substr(len + 3, 2);
        }
        return size_str;
    },
    /**
     * 根据当前国际化语言取字段
     * @param obj
     * @param lang
     * @returns String
     */
    getFieldByLang: function(obj:any, lang:string) {
        // if (!lang) {
        //     const { appContext } = getCurrentInstance() as ComponentInternalInstance
        //     const proxy = appContext.config.globalProperties;
        //     lang = proxy.$i18n.global.locale;
        // }
        // return obj?obj[lang]:"";
    }
}

export default (app:App)=>{
    app.config.globalProperties.$utils = utils;
    app.config.globalProperties.$getFieldByLang = utils.getFieldByLang;
}
